package logic

import (
	"common/comCopier"
	"common/dao/model"
	"common/dao/query"
	"context"
	"github.com/jinzhu/copier"

	"gateway/internal/svc"
	"gateway/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
	return &UserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfoLogic) UserInfo() (resp *types.UserInfo, err error) {
	// todo: add your logic here and delete this line

	tokenInfo := l.ctx.Value("token")
	tokenUser := model.UserShop{}
	err = copier.Copy(&tokenUser, tokenInfo)
	if err != nil {
		return nil, err
	}

	q := query.Use(l.svcCtx.DB).SysUser
	u, err := q.WithContext(l.ctx).Where(q.UUID.Eq(tokenUser.UUID)).First()
	if err != nil {
		return nil, err
	}
	resp = &types.UserInfo{}
	err = comCopier.CopyWithOption(&resp, u)
	if err != nil {
		return nil, err
	}
	//resp.UpdateTime = u.UpdateTime.Format("2006-01-02 15:04:05")
	//resp.UpdateTime = u.UpdateTime.Format("2006-01-02 15:04:05")
	return
}
